<?php

/**
 *  绩效工资
 */

class Controller_pay extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->syllabus = YOU::loadModel('syllabus');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->pay      = YOU::loadModel('pay');
		$this->model->appoint  = YOU::loadModel('appoint');
		$this->model->duty     = YOU::loadModel('duty');
	}


	/** 首页 **/
	public function actionIndex()
	{


		$this->tpl->display();
	}

	/** 全局设置 **/
	public function actionSet()
	{
		if ($_POST){

			$set_classsub   = $this->post->set_classsub;
			$set_unitem     = serialize($this->post->set_unitem);
			$set_uncheck    = serialize($this->post->set_uncheck);
			$set_mchecksub  = $this->post->set_mchecksub;
			$set_lchecksub  = $this->post->set_lchecksub;
			$set_cdutysub   = $this->post->set_cdutysub;
			$set_sdutysub   = $this->post->set_sdutysub;
			$set_dutystart  = $this->post->set_dutystart;
			$set_dutyend    = $this->post->set_dutyend;
			$set_dutyarea    = $this->post->set_dutyarea;
			$hour_isweek    = $this->post->hour_isweek;

			$set_hoursub    = $this->post->set_hoursub;
			$set_hourlimit  = $this->post->set_hourlimit;
			$set_uphoursub  = $this->post->set_uphoursub;
			$set_adjustsub  = $this->post->set_adjustsub;
			foreach ($this->free_ini as $key => $value){
				$set_adjustcost[$value] = $_POST["set_adjustcost_{$value}"];
			}
			$set_adjustcost = serialize($set_adjustcost);
			$set_dutyarea   = serialize($set_dutyarea);
			$info = array(
			"set_classsub"   => $set_classsub,
			"set_unitem"     => $set_unitem,
			"set_uncheck"    => $set_uncheck,
			"set_mchecksub"  => $set_mchecksub,
			"set_lchecksub"  => $set_lchecksub,
			"set_cdutysub"   => $set_cdutysub,
			"set_sdutysub"   => $set_sdutysub,
			"set_dutystart"  => $set_dutystart,
			"set_dutyend"    => $set_dutyend,
			"set_dutyarea"   => $set_dutyarea,
			"hour_isweek"    => $hour_isweek,
			"set_hoursub"    => $set_hoursub,
			"set_hourlimit"  => $set_hourlimit,
			"set_uphoursub"  => $set_uphoursub,
			"set_adjustsub"  => $set_adjustsub,
			"set_adjustcost" => $set_adjustcost,
			);

			// 修改设置
			$this->model->pay->set($info);
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=pay.set");

		}

		$set_data = $this->model->pay->getSet();
		foreach ($this->free_ini as $key => $value){
			$set_data['list_uncheck'][$key]['name'] = $value;
			if (@in_array($key, $set_data['set_unitem'])){
				$set_data['list_uncheck'][$key]['check'] = 1;
			}
			$set_data['list_adjustcost'][$value] = $set_data['set_adjustcost'][$value];
		}
		foreach ($this->pay_unitem as $key => $value){
			$set_data['list_unitem'][$key]['name'] = $value;
			if (@in_array($key, $set_data['set_unitem'])){
				$set_data['list_unitem'][$key]['check'] = 1;
			}
		}

		// 最新学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];
		// 本次id
		$box_id = $this->model->duty->getBox($semester_id);
		// 读出值日位置
		$duty_lists = $this->model->duty->getAllByBox($box_id);
		foreach ($duty_lists as $key => $value){
			if (!empty($value['duty_area'])){
				$area_lists[$value['duty_area']]++;
			}
		}


		if(empty($set_data['set_dutyarea'])){

			$set_data['set_dutyarea'] = array();
		}


		$this->tpl->assign("set_dutyarea", $set_data['set_dutyarea']);
		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("area_lists", $area_lists);
		$this->tpl->display("pay/pay.set.tpl.php");
	}

	/** 职务补贴设置 **/
	public function actionJob()
	{
		$set_data = $this->model->pay->getSet();
		$part = $set_data['set_jobsub'];


		if ($_POST){
			$staff_id = $this->post->staff_id;
			$job_sub  = $this->post->job_sub;


			// 重洗数组
			$part[$staff_id] = $job_sub;
			$part = serialize($part);

			// 更新配置信息
			$info = array(
			"set_jobsub" => $part,
			);
			$this->model->pay->set($info);

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=pay.job");

		}

		// 教师列表
		$staff_lists = $this->model->staff->getAll();
		// 补贴信息
		foreach ($staff_lists as $key => $value){
			//  教师补贴
			if($part[$value['staff_id']]){
				$staff_lists[$key]['job_sub'] = $part[$value['staff_id']];
			}
			// 教师职务
			$tmp = $this->model->appoint->getPositionByStaff($value['staff_id']);

			$position = "";
			if (!empty($tmp)){
				foreach ($tmp as $k => $v){
					$position[] = $v;

				}
				$position = implode(",", $position);
			}
			$staff_lists[$key]['staff_job'] = $position;

		}

		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->display("pay/pay.job.tpl.php");
	}

	/** 特殊人员补贴设置 **/
	public function actionSpecial()
	{
		$set_data = $this->model->pay->getSet();
		$part = $set_data['set_specialsub'];


		if ($_POST){
			$staff_id     = $this->post->staff_id;
			$special_sub  = $this->post->special_sub;


			// 重洗数组
			$part[$staff_id] = $special_sub;
			$part = serialize($part);

			// 更新配置信息
			$info = array(
			"set_specialsub" => $part,
			);
			$this->model->pay->set($info);

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=pay.special");

		}

		// 教师列表
		$staff_lists = $this->model->staff->getAll();
		// 补贴信息
		foreach ($staff_lists as $key => $value){
			//  教师补贴
			if($part[$value['staff_id']]){
				$staff_lists[$key]['special_sub'] = $part[$value['staff_id']];
			}

		}

		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->display("pay/pay.special.tpl.php");
	}

	/** 不计常规补贴人员设置 **/
	public function actionUnsub()
	{
		$set_data = $this->model->pay->getSet();

		if ($_POST){

			$set_unsub = $this->post->set_unsub;
			$set_unsub = serialize($set_unsub);

			$info = array(
			"set_unsub" => $set_unsub,
			);

			$this->model->pay->set($info);

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=pay.Unsub");
		}

		$part = (array)$set_data['set_unsub'];

		// 教师列表
		$staff_lists = $this->model->staff->getAll();
		// 选中
		foreach ($staff_lists as $key => $value){
			if (@in_array($value['staff_id'], $part)){
				$staff_lists[$key]['check'] = 1;
			}
		}

		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->display("pay/pay.unsub.tpl.php");
	}




	/** 各年级作业补贴设置 list **/
	public function actionWorkList()
	{
		// 年级列表
		$grade_lists = $this->model->grade->getIni();

		// 部门列表
		$subject = $this->model->subject->getIni();

		$data_lists = array();
		foreach ($grade_lists as $key => $value){

			$data_lists[$key]['grade_name'] = $value;
			$work = $this->model->pay->getWork($key);
			// 学科列表
			$tmp = unserialize($work['work_subject']);
			$part = "";
			if (!empty($tmp)){
				foreach ($tmp as $k => $v){
					$part[] = $subject[$v];
				}
				$part = implode(",", $part);
			}

			$data_lists[$key]['work_subject'] = $part;
			$data_lists[$key]['first_sub'] = $work['first_sub'];
			$data_lists[$key]['other_sub'] = $work['other_sub'];
		}


		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->display("pay/pay.worklist.tpl.php");
	}


	/** 各年级作业补贴设置 update **/
	public function actionWorkUpdate()
	{
		$grade_id = $this->get->grade_id;
		$work_data = $this->model->pay->getWork($grade_id);

		if ($_POST){
			$first_sub    = $this->post->first_sub;
			$other_sub    = $this->post->other_sub;
			$work_subject = $this->post->work_subject;

			$work_subject = serialize($work_subject);


			// 是否存在 否则修改
			if (empty($work_data)){
				$info = array(
				"grade_id"     => $grade_id,
				"work_subject" => $work_subject,
				"first_sub"    => $first_sub,
				"other_sub"    => $other_sub,
				"status"       => 1,
				"create_time"  => date("Y-m-d H:i:s"),
				"update_time"  => "0000-00-00 00:00:00"
				);
				$this->db->do_insert("tbl_pay_work", $info);
			}else{
				$info = array(
				"work_subject" => $work_subject,
				"first_sub"    => $first_sub,
				"other_sub"    => $other_sub,
				"status"       => 1,
				"update_time"  => date("Y-m-d H:i:s"),
				);
				$this->db->do_update("tbl_pay_work", $info, "work_id='{$work_data['work_id']}'");

			}

			Helper_Client::location("修改成功", "index.php?a=pay.worklist");

		}

		// 年级信息
		$grade_data = $this->model->grade->getInfo($grade_id);

		// 学科列表
		$subject_lists = $this->model->subject->getAll();
		// 学科选中
		$part = (array)unserialize($work_data['work_subject']);

		foreach ($subject_lists as $key => $value){
			if (@in_array($value['subject_id'], $part)){
				$subject_lists[$key]['check'] = 1;
			}
		}


		$this->tpl->assign("work_data", $work_data);
		$this->tpl->assign("grade_data", $grade_data);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->display("pay/pay.workupdate.tpl.php");
	}


	/** 月 **/
	public function actionMonthList()
	{

		$semester_id = $this->get->semester_id;
		$pay_month   = $this->get->pay_month;
		if(empty($pay_month)){
			$row = $this->model->semester->getWeekByTime(date("Y-m-d"));
			$pay_month = $row['semester_month'];
		}
		$semester_data = $this->model->semester->getInfo($semester_id);

		// 最新学期id
		$semester_new = $this->model->semester->getNew();

		if (empty($semester_data) || $semester_id == $semester_new['semester_id']){

			$semester_id   = $semester_new['semester_id'];
			$this->tpl->assign("semester_now", "1");
		}
		// 通过当前时间获得当前学月
		$times = date('Y-m-d',time());
		$now_month = $this->model->semester->getWeekByTimes($times);
		$now_month = $now_month['semester_month'];

		if (empty($pay_month) || $now_month == $pay_month){
			$pay_month = $now_month;
			$this->tpl->assign("month_now", "1");
		}
		if(empty($pay_month)){
			$row = $this->model->semester->getWeekByTime(date("Y-m-d"));
			$pay_month = $row['semester_month'];
		}

		// 本次id
		$box_id = $this->model->duty->getBox($semester_id);


		// 教师列表
		$staff_lists = $this->model->staff->getAll();
		// 查询是否已存在本学期
		$row = $this->model->pay->getMonthInfo($semester_id, $pay_month);

		$pay_data = unserialize($row['pay_data']);

		foreach ($staff_lists as $key => $value){
			$info = array();
			if ($pay_data[$value['staff_id']]){
				$info = $pay_data[$value['staff_id']];
				$data_lists[$value['staff_id']] = $info;

				//  实发 不算节数 会旗
				$all = $info;
				unset($all['costa']);
				unset($all['costc']);
				$data_lists[$value['staff_id']]['cash'] = array_sum($all)-$info['total'];
			}else{
				$data_lists[$value['staff_id']] = "";
			}
			$data_lists[$value['staff_id']]['staff_name'] = $value['staff_name'];
			$data_lists[$value['staff_id']]['staff_number'] = $value['staff_number'];
		}

		// 学期列表
		$semester_lists = $this->model->semester->getIni();
		$month_lists = $this->model->pay->getMonthAll();

		$this->tpl->assign("pay_month", $pay_month);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("semester_lists", $semester_lists);

		$this->tpl->assign("month_lists", $month_lists);
		$this->tpl->assign("out_lists", $data_lists);
		$this->tpl->display("pay/pay.monthlist.tpl.php");
	}

	/** 月修改 **/
	public function actionMonthUpdate()
	{

		// 最新学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		//查找对应学月
		$pay_month = $this->get->month;
		if(empty($pay_month)){
			$row = $this->model->semester->getWeekByTime(date("Y-m-d"));
			$pay_month = $row['semester_month'];
		}

		// 查询是否已存在本学期本月
		$row = $this->model->pay->getMonthInfo($semester_id, $pay_month);

		// 配置
		$set_data = $this->model->pay->getSet();

		// 教师列表
		$staff_lists = $this->model->staff->getAll();

		if($_POST){

			$data = array();
			foreach ($staff_lists as $key => $value){
				$tmp = array(
				"class"  => $_POST["class_{$value['staff_id']}"],
				"job"     => $_POST["job_{$value['staff_id']}"],
				"check"   => $_POST["check_{$value['staff_id']}"],
				"duty"    => $_POST["duty_{$value['staff_id']}"],
				"home"    => $_POST["home_{$value['staff_id']}"],
				"hour"    => $_POST["hour_{$value['staff_id']}"],
				"uphour"  => $_POST["uphour_{$value['staff_id']}"],
				"adjust"  => $_POST["adjust_{$value['staff_id']}"],
				"other"   => $_POST["other_{$value['staff_id']}"],
				);
				$tmp['total'] = array_sum($tmp);
				$tmp["costa"]   = "-".abs((int)$_POST["costa_{$value['staff_id']}"]);
				$tmp["costb"]   = "-".abs((int)$_POST["costb_{$value['staff_id']}"]);
				$tmp["costc"]   = "-".abs((int)$_POST["costc_{$value['staff_id']}"]);
				$tmp["costd"]   = "-".abs((int)$_POST["costd_{$value['staff_id']}"]);
				$tmp["coste"]   = "-".abs((int)$_POST["coste_{$value['staff_id']}"]);

				$data[$value['staff_id']] = $tmp;
			}

			// 写入数据库
			$info = array(
			"pay_type" => "month",
			"pay_data" => serialize($data),
			"status"   => 1,
			);

			if (!empty($row)){
				$info['update_time'] = date("Y-m-d H:i:s");
				$this->db->do_update("tbl_pay", $info, "pay_id='{$row['pay_id']}'");

			}else{
				$info['semester_id'] = $semester_id;
				$info['pay_month']   = $pay_month;
				$info['create_time'] = date("Y-m-d H:i:s");
				$this->db->do_insert("tbl_pay", $info);
			}


			// 跳转
			Helper_Client::location("修改成功", "index.php?a=pay.monthlist&semester_id={$semester_id}&month={$pay_month}");

		}


		// 关联之前记录
		$pay_data = unserialize($row['pay_data']);

		// 职务补贴列表
		$set_job  = $set_data['set_jobsub'];

		//  班主任列表
		$mc_lists = $this->model->appoint->getTeacherAll();
		foreach ($mc_lists as $key => $value){
			$student_count = $this->model->class->countStudent($value['class_id']);
			$mc[$value['staff_id']] = $set_data['set_classsub']*$student_count;
		}

		// 没有请假的为全勤

		// 作业补贴


		// 课时补贴

		/*
		//全勤人
		$attend_lists = $this->db->fetchAll("SELECT * FROM `tbl_attend` WHERE semester_id ='{$semester_id}' AND attend_smonth = '{$pay_month}' AND attend_a=0 AND attend_b=0 AND attend_c=0 AND attend_d=0 AND attend_e=0 AND attend_f=0 AND attend_g=0 AND attend_h=0");
		$attend_ids = array();
		foreach ($attend_lists as $key => $value){
		$attend_ids[] = $value['staff_id'];
		}

		*/


		//本学月值日
		$time = $this->model->semester->getMonthTimes($semester_id,$pay_month);
		//有特殊值日位置
		if(!empty($set_data['set_dutyarea'])){
			$where = array();
			foreach ($set_data['set_dutyarea'] as $key => $value){
				$where[]= " duty_area like '{$value}%' ";
			}
			$where = implode(" OR ",$where);
			$sduty_lists = $this->db->fetchAll("SELECT *,count(1) as count FROM `tbl_duty` WHERE ( {$where} ) AND semester_id ='{$semester_id}' AND start_time>='{$time['start_time']}' AND start_time<'{$time['end_time']}' GROUP BY staff_id");
			$sdutys = array();
			foreach ($sduty_lists as $key => $value){
				$sdutys[$value['staff_id']] = $value['count'];
				//and!!!
			}
			$where = array();
			foreach ($set_data['set_dutyarea'] as $key => $value){
				$where[]= " duty_area not like '{$value}%' ";
			}
			$where = implode(" AND ",$where);
			$duty_lists = $this->db->fetchAll("SELECT *,count(1) as count FROM `tbl_duty` WHERE ( {$where} ) AND semester_id ='{$semester_id}' AND start_time>='{$time['start_time']}' AND start_time<'{$time['end_time']}' GROUP BY staff_id");
			$dutys = array();
			foreach ($duty_lists as $key => $value){
				$dutys[$value['staff_id']] = $value['count'];
				//and!!!
			}
		}else{

			$sdutys = array();
			//只有普通值日
			$duty_lists = $this->db->fetchAll("SELECT *,count(1) as count FROM `tbl_duty` WHERE semester_id ='{$semester_id}' AND start_time>='{$time['start_time']}' AND start_time<'{$time['end_time']}' GROUP BY staff_id");
			$dutys = array();
			foreach ($duty_lists as $key => $value){
				$dutys[$value['staff_id']] = $value['count'];
			}
		}


		// 不计全勤奖设置
		$set_all = array(

		1 => 'e',
		2 => 'f',
		3 => 'c',
		4 => 'd',
		5 => 'g',
		6 => 'h',

		);
	
		// 不计全勤奖设置
		foreach ($set_all as $key => $value){
			if (@in_array($key,$set_data['set_uncheck'])) {
				$wheres .=" OR attend_{$value} !='0'";
			}
		}
	
		$set_unitem = $set_data['set_unitem'];
		$set_unsub  = $set_data['set_unsub'];
		// 作业补贴
		$work_lists= $this->db->fetchAll("SELECT * FROM `tbl_pay_work`");

		foreach ($work_lists as $key => $value){
			// 反序列化
			$work_lists['work_subject'] =unserialize($value['work_subject']);
			foreach ($work_lists['work_subject'] as $k => $v){
				$m = $value['grade_id']._.$v;
				$bba[$m][1]  = $value['first_sub'];
				$bba[$m][2]  = $value['other_sub'];
			}

		}

		foreach ($staff_lists as $key => $value){

			if ($pay_data[$value['staff_id']]){
				$info = $pay_data[$value['staff_id']];
				$data_lists[$value['staff_id']] = $info;
			}else{
				// 1=班主任补贴
				if(@in_array(1, $set_unitem)){
					// 是否在名单里
					if(@in_array($value['staff_id'], $set_unsub)){
						$mc[$value['staff_id']]=="";
					}
				}

				$info = array(
				// 职务补贴
				"job" => $set_job[$value['staff_id']],
				// 班主任补贴
				"class" => $mc[$value['staff_id']],
				);
				$data_lists[$value['staff_id']] = $info;

				// 作业补贴
				// 重组数据（班级、年级）
				$bbs = array();
				$class_list = $this->db->fetchAll("SELECT * FROM `tbl_staff_subject` WHERE staff_id = {$value['staff_id']}  ");

				if (!empty($class_list)) {
					foreach ($class_list as $k => $v){
						$m = $v['grade_id'].'_'.$v['subject_id'];
						$bbs[$m]++;
					}
				}

				$work_money = 0;
				if (!empty($bbs)) {
					foreach ($bbs as $k => $v){
						if (!empty($bba[$k])){
							if ($v < 2){
								$work_money += $bba[$k][1];
							}else{
								//$work_money += ($bba[$k][2]* $v) - abs(($bba[$k][2]-$bba[$k][1]));
								$work_money += ($bba[$k][2] + $bba[$k][1]) * ($v-1) + $bba[$k][1];
							}

						}
					}
				}
				$data_lists[$value['staff_id']]['home'] = $work_money;


				// 全勤是否计补贴
				$nocheck = 0;
				
				
				// 是否全勤
				$attend_data = $this->db->fetchRow("SELECT staff_id FROM `tbl_attend` WHERE semester_id ='{$semester_id}' AND attend_smonth='{$pay_month}' AND staff_id='{$value['staff_id']}' AND (attend_a !='0' OR attend_b !='0' {$wheres} OR attend_i !='0' OR attend_j !='0')");
				
				//是否全勤
				if(empty($attend_data) ){

					$data_lists[$value['staff_id']]['check'] = $set_data['set_mchecksub'];
				}
				
				//值日的薪水
				$data_lists[$value['staff_id']]['duty'] = $set_data['set_cdutysub']*$dutys[$value['staff_id']]+$set_data['set_sdutysub']*$sdutys[$value['staff_id']];


				//1获取周课时 2算课时费
				$row1 = $this->db->fetchRow("SELECT count(1) as count FROM `tbl_syllabus` WHERE staff_id='{$value['staff_id']}' AND semester_id='{$semester_id}'");
				$weekcount = $row1['count'];
				if($weekcount>$set_data['set_hourlimit']){
					$over = $weekcount - $set_data['set_hourlimit'];
					$data_lists[$value['staff_id']]['hour'] = $set_data['set_hoursub']*$set_data['set_hourlimit'];
					$data_lists[$value['staff_id']]['uphour'] = $set_data['set_uphoursub']*$over;
				}else{
					$data_lists[$value['staff_id']]['hour'] = $set_data['set_hoursub']*$weekcount;
					$data_lists[$value['staff_id']]['uphour'] = 0;
				}
				//代课费用   1代课课时2费用
				$row1 = $this->db->fetchRow("SELECT count(1) as count FROM `tbl_absent` WHERE to_staff='{$value['staff_id']}' AND semester_id='{$semester_id}' AND absent_smonth={$pay_month}");
				$adjustcount = (int)$row1['count'];
				$data_lists[$value['staff_id']]['adjust'] = $set_data['set_adjustsub']*$adjustcount;
			}
			$data_lists[$value['staff_id']]['staff_name'] = $value['staff_name'];
			$data_lists[$value['staff_id']]['staff_number'] = $value['staff_number'];
			
			// 1=班主任
			if(@in_array(1, $set_unitem)){
				// 是否在名单里
				if(@in_array($value['staff_id'], $set_unsub)){
					$data_lists[$value['staff_id']]['class'] = "0";
				}
			}
			// 2=全勤
			if(@in_array(2, $set_unitem)){
				// 是否在名单里
				if(@in_array($value['staff_id'], $set_unsub)){
					$data_lists[$value['staff_id']]['check'] = 0;
					
				}
			}
			// 3=值日
			if(@in_array(3, $set_unitem)){
				// 是否在名单里
				if(@in_array($value['staff_id'], $set_unsub)){
					$data_lists[$value['staff_id']]['duty'] = "0";
				}
			}
			// 4=作业
			if(@in_array(4, $set_unitem)){
				// 是否在名单里
				if(@in_array($value['staff_id'], $set_unsub)){
					$data_lists[$value['staff_id']]['home'] = "0";
				}
			}
			// 5=课时
			if(@in_array(5, $set_unitem)){
				// 是否在名单里
				if(@in_array($value['staff_id'], $set_unsub)){
					$data_lists[$value['staff_id']]['hour']="0";
				}
			}
			// 6=代课
			if(@in_array(6, $set_unitem)){
				// 是否在名单里
				if(@in_array($value['staff_id'], $set_unsub)){
					$data_lists[$value['staff_id']]['uphour'] = "0";
				}
			}
			// 7=超课时
			if(@in_array(7, $set_unitem)){
				// 是否在名单里
				if(@in_array($value['staff_id'], $set_unsub)){
					$data_lists[$value['staff_id']]['adjust'] = "0";
				}
			}






		}
		$i = 1;
		foreach ($data_lists as $key => $value){
			$out_lists[$i]["staff_id"] = $key;
			$out_lists[$i]["lists"] = $value;
			$i++;
		}


		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("pay_month", $pay_month);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("out_lists", $out_lists);
		$this->tpl->display("pay/pay.monthupdate.tpl.php");
	}


	/** 期末 **/
	public function actionEndUpdate()
	{
		// 最新学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 本次id
		$box_id = $this->model->duty->getBox($semester_id);


		// 教师列表
		$staff_lists = $this->model->staff->getAll();
		// 查询是否已存在本学期
		$row = $this->model->pay->getEndInfo($semester_id);
		if($_POST){

			$data = array();
			foreach ($staff_lists as $key => $value){
				$tmp = array(
				"teach"   => $_POST["teach_{$value['staff_id']}"],
				"duty"    => $_POST["duty_{$value['staff_id']}"],
				"class"   => $_POST["class_{$value['staff_id']}"],
				"open"    => $_POST["open_{$value['staff_id']}"],
				"single"  => $_POST["single_{$value['staff_id']}"],
				"student" => $_POST["student_{$value['staff_id']}"],
				"finish"  => $_POST["finish_{$value['staff_id']}"],
				"other"   => $_POST["other_{$value['staff_id']}"],
				);
				$data[$value['staff_id']] = $tmp;
			}

			// 写入数据库
			$info = array(
			"pay_type" => "end",
			"pay_data" => serialize($data),
			"status" => 1,
			);

			if (!empty($row)){
				$info['update_time'] = date("Y-m-d H:i:s");
				$this->db->do_update("tbl_pay", $info, "pay_id='{$row['pay_id']}'");

			}else{
				$info['semester_id'] = $semester_id;
				$info['create_time'] = date("Y-m-d H:i:s");
				$this->db->do_insert("tbl_pay", $info);
			}


			// 跳转
			Helper_Client::location("修改成功", "index.php?a=pay.endlist&semester_id={$semester_id}");

		}

		// 关联之前记录
		$pay_data = unserialize($row['pay_data']);

		foreach ($staff_lists as $key => $value){
			// 是否全勤

			if ($pay_data[$value['staff_id']]){
				$info = $pay_data[$value['staff_id']];
				$data_lists[$value['staff_id']] = $info;
			}else{
				$data_lists[$value['staff_id']] = "";
			}
			$data_lists[$value['staff_id']]['staff_name'] = $value['staff_name'];
			$data_lists[$value['staff_id']]['staff_number'] = $value['staff_number'];
		}
		$i = 1;
		foreach ($data_lists as $key => $value){
			$out_lists[$i]["staff_id"] = $key;
			$out_lists[$i]["lists"] = $value;
			$i++;
		}


		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("out_lists", $out_lists);
		$this->tpl->display("pay/pay.endupdate.tpl.php");
	}


	public function actionEndList()
	{
		$semester_id = $this->get->semester_id;
		$semester_data = $this->model->semester->getInfo($semester_id);
		if (empty($semester_data)){
			// 最新学期id
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
			$this->tpl->assign("semester_now", "1");
		}
		// 本次id
		$box_id = $this->model->duty->getBox($semester_id);


		// 教师列表
		$staff_lists = $this->model->staff->getAll();
		// 查询是否已存在本学期
		$row = $this->model->pay->getEndInfo($semester_id);



		$pay_data = unserialize($row['pay_data']);

		foreach ($staff_lists as $key => $value){
			$info = array();
			if ($pay_data[$value['staff_id']]){
				$info = $pay_data[$value['staff_id']];
				$data_lists[$value['staff_id']] = $info;
				$data_lists[$value['staff_id']]['total'] = array_sum($info);
			}else{
				$data_lists[$value['staff_id']] = "";
			}
			$data_lists[$value['staff_id']]['staff_name'] = $value['staff_name'];
			$data_lists[$value['staff_id']]['staff_number'] = $value['staff_number'];
		}

		// 学期列表
		$semester_lists = $this->model->semester->getIni();

		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("out_lists", $data_lists);
		$this->tpl->display("pay/pay.endlist.tpl.php");
	}

}

?>